<?php

Class SystemAction extends BaseAction{
   /*
    +----------------------------------------------------------
    * 显示数据缓存和昵称库管理入口
    +----------------------------------------------------------
    */	
	public function index(){
		$this->display();
	}
	
   /*
    +----------------------------------------------------------
    * 数据缓存更新
    +----------------------------------------------------------
    */
	public function runtimeUpdate(){
		$this->display();
	}
	
	public function runtimeClear(){
		import("Web.ORG.Io.Dir");
		$id = $_POST['id'];
		
		if(in_array('cache', $id))
			$file['cache'] = array("Admin/Runtime/Cache","Web/Runtime/Cache");
		if(in_array('data', $id))
			$file['data'] = array("Admin/Runtime/Data","Web/Runtime/Data");
		if(in_array('temp', $id))
			$file['temp'] = array("Admin/Runtime/Temp","Web/Runtime/Temp");
		if(in_array('logs', $id))
			$file['logs'] = array("Admin/Runtime/Logs","Web/Runtime/Logs");
		foreach($file as $key=>$value){
			Dir::delDir($file[$key][0]);
			Dir::delDir($file[$key][1]);
		}	
		$this->success('缓存已更新!');
	}
	
   /*
    +----------------------------------------------------------
    * 昵称库管理
    +----------------------------------------------------------
    */
	public function nicknameManage(){
		import("Web.ORG.Util.Page");
		
		$Nickname = D('Nickname');
		$count = $Nickname->count();
		$Page = new Page($count);
		$show = $Page->show();
		$nickname = $Nickname->order('name_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('nickname',$nickname);
		$this->assign('pages',$show);
		$this->display();
	}
	
	public function add(){
		$this->assign('select','add');
		$this->display('nicknamemanage');
	}
	
	//添加新的昵称
	public function adds(){
		$choice = $_POST['choice'];
		empty ($choice) && $this->error('请选择添加单个昵称或者多个昵称!');
		switch($choice){
			//添加一个昵称
			case 'single':
				$data['nickname'] = $_POST['nickname'];
				$data['status'] = 1;
				empty($data['nickname']) && $this->error('请填写昵称!');
				$Nickname = D('Nickname');
				$exist = $Nickname->where("nickname = ".'"'.$data['nickname'].'"')->find();
				if(!$exist){
					$result = $Nickname->data($data)->add();
				}else $this->error('该昵称已存在!');
				if($result)
					$this->success('添加完毕!');
				else $this->error('操作失败，请重试!');
				break;
				
			//批量添加昵称
			case 'some':
				//上传文件
				$file = $_FILES['file'];
				if(!empty($file['name'])) $file_name = $this->_upload("nickname_files",false,true);
				
				//读取文件内容并添加到昵称库
				$file_path = $_SERVER['DOCUMENT_ROOT'];
				$fp = fopen("$file_path/Attachments/nickname_files/$file_name", 'r');
				if($fp){
					while(!feof($fp)){
						$info = fgets($fp);
						$map = explode(",",$info);
						$data['nickname'] = $map[0];
						$data['status'] = $map[1];
						$Nickname = D('Nickname');
						$exist = $Nickname->where("nickname = ".'"'.$data['nickname'].'"')->find();
						if(!$exist)
							$Nickname->data($data)->add();	
					}
					$this->success('添加完毕!');
					//关闭文件
					fclose($fp);
				}
				break;
		}
	}
	
	public function update(){
		$name_id = $_GET['name_id'];
		$name = D('Nickname')->where('name_id ='.$name_id)->find();
		$this->assign('name',$name);
		$this->assign('select','update');
		$this->display('nicknamemanage');
	}
	
	//更新昵称信息
	public function updates(){
		empty($_POST['nickname']) && $this->error('昵称不许空缺!');
		$data['nickname'] = $_POST['nickname'];
		if(1==$_POST['status']){
			$data['status'] = 1;
		}else{
			$data['status'] = 0;
		}
		$nid = $_POST['name_id'];
		D('Nickname')->where('name_id ='.$nid)->data($data)->save();
		$this->success('修改成功!');
	}
	
	//删除昵称
	public function delete(){
		$checkbox = $_POST['id'];
		empty($checkbox) && $this->error('请选择需要删除的昵称!');
		$id = is_array($checkbox)?implode(',',$checkbox):$checkbox;
		$admin = D('Nickname')->execute('DELETE FROM '.C("DB_PREFIX").'nickname where name_id '.' IN ('.$id.')');
		if($admin)
			$this->success('删除成功!');
		else $this->error('操作失败，请重试!');
	}
	
	/*
    +----------------------------------------------------------
    * 邀请码管理
    +----------------------------------------------------------
    */
	
	public function inviteManage(){
		import("Web.ORG.Util.Page");
		
		$Invite = D('Invite');
		$count = $Invite->count();
		$Page = new Page($count);
		$show = $Page->show();
		$invite = $Invite->order('invite_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('invite',$invite);
		$this->assign('pages',$show);
		$this->display();
	}
	
	public function inviteAdd(){
		$this->assign('select','inviteAdd');
		$this->display('invitemanage');
	}
	
	public function inviteAdds(){
		
	}
}
?>
